<?php
    require_once("connect.php");
	require_once("hierarchies.php");
		
	function createNode(){
		global $dbcon;
		
		$id_ = "NULL";
		if($_POST["id"] != "NULL"){
			$id_ = (integer)$_POST["id"];
		}
		$name_ = pg_escape_string(utf8_encode($_POST["name"]));
		$hierarchy_id_ = (integer)$_POST["hierarchy_id"];
		$node_id_ = "NULL";
		if($_POST["node_id"] != "NULL"){
			$node_id_ = (integer)$_POST["node_id"];
		}
		$short_desc_ = pg_escape_string(utf8_encode($_POST["short_desc"]));
		$long_desc_ = pg_escape_string(utf8_encode($_POST["long_desc"]));
		
		$query_ = "INSERT INTO nodes (".($id_ != "NULL" ? "id, " : "").($name_ != "" ? "name, " : "")."hierarchy_id, node_id, short_desc, long_desc) VALUES (".($id_ != "NULL" ? "$id_, " : "").($name_ != "" ? "'$name_', " : "")."$hierarchy_id_, $node_id_, '$short_desc_', '$long_desc_')";
		
		pg_query($dbcon, $query_);
	}
	
	function updateNode(){
		global $dbcon;
		
		$id_ = "NULL";
		if($_POST["id"] != "NULL"){
			$id_ = (integer)$_POST["id"];
		}
		$name_ = pg_escape_string(utf8_encode($_POST["name"]));
		$hierarchy_id_ = (integer)$_POST["hierarchy_id"];
		$node_id_ = "NULL";
		if($_POST["node_id"] != "NULL"){
			$node_id_ = (integer)$_POST["node_id"];
		}
		$short_desc_ = pg_escape_string(utf8_encode($_POST["short_desc"]));
		$long_desc_ = pg_escape_string(utf8_encode($_POST["long_desc"]));
		
		$query_ = "UPDATE nodes SET ".($name_ != "name='', " ? "name='$name_', " : "")."hierarchy_id=$hierarchy_id_, node_id=$node_id_, short_desc='$short_desc_', long_desc='$long_desc_' WHERE id=$id_";
		
		echo $query_;
		
		pg_query($dbcon, $query_);
	}
	
	function getNodes(){
		global $dbcon;
		return pg_query($dbcon, "SELECT * FROM nodes ORDER BY id");
	}
	
	function getChildrenNodes($idparam){
		global $dbcon;
		
		$id_ = NULL;
		if(isset($idparam) && $idparam != ""){
			$id_ = (integer)$idparam;
		}
		
		return pg_query($dbcon, "SELECT * FROM nodes WHERE node_id=$id_ ORDER BY id");
	}
	
	function getNode($idparam){
		global $dbcon;
		
		$id_ = (integer)$idparam;
		$query_ = "SELECT * FROM nodes WHERE id = $id_";
		
		$resulthandler_ = pg_query($dbcon, $query_);
		return pg_fetch_assoc($resulthandler_);
	}
	
	function printNodesAsOptions($idparam){
		$id_ = NULL;
		if(isset($idparam) && $idparam != ""){
			$id_ = (integer)$idparam;
		}
		
		$hiers_ = getHierarchies();
		$hierarchies_;
		while($h_ =  pg_fetch_assoc($hiers_)){
			$hierarchies_[$h_["id"]] = $h_;
		}
		
		$resulthandler_ = getNodes();
		while($node_ = pg_fetch_assoc($resulthandler_)){
			if($node_["id"] == $id_){
				echo "<option value=\"".$node_['id']."\" selected>".$hierarchies_[$node_["hierarchy_id"]]["name"]." ".$node_['name']."</option>";
			}
			else{
				echo "<option value=\"".$node_['id']."\">".$hierarchies_[$node_["hierarchy_id"]]["name"]." ".$node_['name']."</option>";
			}
		}
	}
	
	function printChildrenNodesAsOptions($idparam){
		$id_ = NULL;
		if(isset($idparam) && $idparam != ""){
			$id_ = (integer)$idparam;
		}
		
		$hiers_ = getHierarchies();
		$hierarchies_;
		while($h_ =  pg_fetch_assoc($hiers_)){
			$hierarchies_[$h_["id"]] = $h_;
		}
		
		$resulthandler_ = getChildrenNodes($id_);
		while($node_ = pg_fetch_assoc($resulthandler_)){
			echo "<option value=\"".$node_['id']."\">".$hierarchies_[$node_["hierarchy_id"]]["name"]." ".$node_['name']."</option>";
		}
	}
	
	function printRootNodesAsOptions(){
		global $dbcon;
		$hiers_ = getHierarchies();
		$hierarchies_;
		while($h_ =  pg_fetch_assoc($hiers_)){
			$hierarchies_[$h_["id"]] = $h_;
		}
		
		$resulthandler_ = pg_query($dbcon, "SELECT * FROM nodes WHERE node_id is NULL");
		while($node_ = pg_fetch_assoc($resulthandler_)){
			echo "<option value=\"".$node_['id']."\">".$hierarchies_[$node_["hierarchy_id"]]["name"]." ".$node_['name']."</option>";
		}
	}
	
	function deleteNode($idparam){
		global $dbcon;
		
		$id_ = (integer)$idparam;
		pg_query($dbcon, "DELETE FROM nodes WHERE id=$id_");
	}
?>